local pianan = fk.CreateSkill {
  name = "lingling__pianan",
}

Fk:loadTranslationTable {
  ["lingling__pianan"] = "偏安",
  [":lingling__pianan"] = "当你每回合首次成为基本牌/锦囊牌的目标后，你可以令使用者摸一张牌或重铸一张牌，然后其本回合不能对你使用锦囊牌/基本牌。",

  ["#lingling__pianan-invoke"] = "偏安：你可以令 %dest 摸一张牌或重铸一张牌，其本回合不能对你使用%arg",
  ["#lingling__pianan-choice"] = "偏安：你可以令 %dest 执行一项",
  ["lingling__pianan-draw"] = "令其摸一张牌",
  ["lingling__pianan-recast"] = "令其重铸一张牌",
  ["#lingling__pianan-recast"] = "偏安：请重铸一张牌",
}

pianan:addEffect(fk.TargetConfirmed, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(pianan.name) and
        data.card.type ~= Card.TypeEquip and not data.from.dead and
        not table.contains(player:getTableMark("lingling__pianan-turn"), data.card.type) then
      player.room:addTableMark(player, "lingling__pianan-turn", data.card.type)
      local use_events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data
        return use.card.type == data.card.type and table.contains(use.tos, player)
      end, Player.HistoryTurn)
      return #use_events == 1 and use_events[1].data.card == data.card
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
          skill_name = pianan.name,
          prompt = "#lingling__pianan-invoke::" .. data.from.id .. ":" .. (data.card.type == Card.TypeBasic and "trick" or "basic"),
        }) then
      event:setCostData(self, { tos = { data.from } })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local type = data.card.type == Card.TypeBasic and "trick" or "basic"
    room:addTableMark(data.from, "lingling__pianan_" .. type .. "-turn", player.id)
    local choices = { "lingling__pianan-draw" }
    if not data.from:isNude() then
      table.insert(choices, 2, "lingling__pianan-recast")
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = pianan.name,
      prompt = "#lingling__pianan-choice::" .. data.from.id,
      all_choices = { "lingling__pianan-draw", "lingling__pianan-recast" },
    })
    if choice == "lingling__pianan-recast" then
      local card = room:askToCards(data.from, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = pianan.name,
        cancelable = false,
        prompt = "#lingling__pianan-recast",
      })
      room:recastCard(card, data.from, pianan.name)
    elseif choice == "lingling__pianan-draw" then
      data.from:drawCards(1, pianan.name)
    end
  end,
})

pianan:addEffect("prohibit", {
  is_prohibited = function(self, from, to, card)
    return to and card and
        table.contains(from:getTableMark("lingling__pianan_" .. card:getTypeString() .. "-turn"), to.id)
  end,
})

return pianan
